package com.lemon.smartsafety.ui.adapter.fire;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.result.fire.SafeManageDetails;
import com.lemon.smartsafety.utils.CommonUtil;
import com.lemon.smartsafety.utils.common.StringUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 项目名称：workspace
 * 类描述：
 * 创建人：Fargo
 * 创建时间：2016/8/9 16:23
 * 修改人：Fargo
 * 修改时间：2016/8/9 16:23
 * 修改备注：
 */
public class FirefightingSafeAdapter extends BaseAdapter {

    /**
     * TAG
     */
    private final String TAG = "FirefightingManageAdapter";

    /**
     * 选择项
     */
    private int m_nSelectItem = -1;

    /**
     * 数据列表
     */
    private List<SafeManageDetails> mList;


    /**
     * 布局泵
     */
    private LayoutInflater m_layInflater = null;

    private Context mContext;

    //是否显示右箭头
    private boolean isShowArrowRight=true;


    public boolean isShowArrowRight() {
        return isShowArrowRight;
    }

    public void setShowArrowRight(boolean showArrowRight) {
        isShowArrowRight = showArrowRight;
    }


    /**
     * 设置选择项
     *
     * @param n
     */
    public void setSelectItem(final int n) {
        m_nSelectItem = n;
    }

    /**
     * 设置数据列表
     */
    public void setList(ArrayList<SafeManageDetails> list) {
        this.mList = list;
    }

    /**
     * 构造函数
     *
     * @param context
     */
    public FirefightingSafeAdapter(Context context, List<SafeManageDetails> list) {
        try {
            this.mContext = context;

            this.mList = list;

            // 布局泵
            m_layInflater = LayoutInflater.from(context);

        } catch (Exception e) {
        }
    }

    @Override
    public int getCount() {
        try {

           if (mList != null)
                return mList.size();
        } catch (Exception e) {
        }

        return 0;
    }

    @Override
    public Object getItem(int position) {
        try {
            if (mList != null)
                return mList.get(position);
        } catch (Exception e) {
        }

        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressWarnings({"deprecation"})
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        try {

            final ViewHolder holder;
            if (convertView == null) {
                convertView = m_layInflater.inflate(R.layout.item_fire_safe, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }


            if(isShowArrowRight){
                holder.arrowRight.setVisibility(View.VISIBLE);
            }else{
                holder.arrowRight.setVisibility(View.INVISIBLE);
            }

            holder.txtDate.setText(mList.get(position).getCheck_freq()+mList.get(position).getCheck_freq_unit_name().trim());
            //holder.txtTime.setText(mList.get(position).getLast_check_time().trim());
            holder.txtName.setText(mList.get(position).getLast_check_user().trim()+"  "+mList.get(position).getLast_check_time().trim());
            holder.txtTitle.setText(mList.get(position).getItem_name().trim());
            if(!CommonUtil.strIsEmpty(mList.get(position).getRemark().trim())){
                holder.txtTitle.setText(holder.txtTitle.getText().toString().trim()+"  ("+mList.get(position).getRemark().trim()+")");
            }


            String strCheckResult="";
            if (!StringUtils.isEmpty(mList.get(position).getCheck_result())) {
                String checkResult = StringUtils.nullValue(mList.get(position).getCheck_result());
                if (checkResult.length() != 0 && checkResult.indexOf(",") != -1) {
                    strCheckResult=checkResult.substring(mList.get(position).getCheck_result().indexOf(",")+1,checkResult.length());
                }
            }


            switch (mList.get(position).getCheck_status()){
                case 1:
                    holder.txtState.setText("已检查,"+strCheckResult);
                    holder.txtState.setTextColor(ContextCompat.getColor(mContext, R.color.colorAppGreen));
                    holder.imageCircle.setBackgroundResource(R.drawable.shape_circle_green);
                    holder.imageCircle.setVisibility(View.VISIBLE);
                    break;
                case 2:
                    holder.txtState.setText("已检查,"+strCheckResult);
                    //holder.txtState.setText("未检查");
                    holder.txtState.setTextColor(ContextCompat.getColor(mContext, R.color.colorAppRed));
                    holder.imageCircle.setBackgroundResource(R.drawable.shape_circle_red);
                    holder.imageCircle.setVisibility(View.VISIBLE);
                    break;
                case 3:
                    if(CommonUtil.strIsEmpty(mList.get(position).getCheck_result())){
                        holder.txtState.setText("未检查");
                    }else {
                        holder.txtState.setText("已超时,请重新检查");
                    }
                    holder.txtState.setTextColor(ContextCompat.getColor(mContext, R.color.colorAppYellow));
                    holder.imageCircle.setBackgroundResource(R.drawable.shape_circle_yellow);
                    holder.imageCircle.setVisibility(View.VISIBLE);
                    break;
                default:
                    holder.txtState.setText("");
                    holder.imageCircle.setVisibility(View.GONE);
                    break;

            }





        } catch (Exception e) {
            e.printStackTrace();
        }

        return convertView;
    }



    static class ViewHolder {
        @Bind(R.id.image_circle)
        ImageView imageCircle;
        @Bind(R.id.arrow_right)
        TextView arrowRight;
        @Bind(R.id.txt_title)
        TextView txtTitle;
        @Bind(R.id.txt_date)
        TextView txtDate;
        @Bind(R.id.txt_name)
        TextView txtName;
        @Bind(R.id.txt_time)
        TextView txtTime;
        @Bind(R.id.txt_state)
        TextView txtState;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }


    /**
     * 项支持器类
     */


}